param(
      $domain = $env:userDNSdomain,
	  $BackupPath = "\\uk01padc006\c$\GPO_Backup"
)
if((Get-Module | Where-Object {$_.Name -eq 'grouppolicy'}) -eq $null) {
	import-module grouppolicy
}

Function Get-UnlinkedGPO() {
	$gpm=New-Object -ComObject gpmgmt.gpm
	$constants = $gpm.GetConstants()
	$gpmDomain = $gpm.GetDomain($domain,$null,$constants.useanydc)
	$gpmSearchCriteria = $gpm.CreateSearchCriteria()
	$gpoList= $gpmDomain.SearchGPOs($gpmSearchCriteria)

	$unlinkedGPO = 0
	foreach($objGPO in $gpoList) {
		$gpmSearchCriteria = $gpm.CreateSearchCriteria()
		$gpmSearchCriteria.add($constants.SearchPropertySomLinks, $constants.SearchOpContains, $objGPO)
		$somList = $gpmDomain.SearchSoms($gpmSearchCriteria)
		if($somList.count -eq 0) { 
			Write-Host "$($objGPO.id) `t $($objGPO.displayname)" 
			$unlinkedGPO +=1
			Backup-GPO -Guid $objGPO.id -Path $BackupPath
			Remove-GPO -Guid $objGPO.id
		}
	}
	if($unlinkedGPO -eq 0) { 
		Write-Host "There are no unlinked GPOs in $domain"
	}
}

Get-UnlinkedGPO